package com.virus.mall.service.impl;

import com.virus.mall.dao.UserDao;
import com.virus.mall.model.User;
import com.virus.mall.model.bo.UpdateUserDataBo;
import com.virus.mall.model.bo.UserLoginBo;
import com.virus.mall.model.bo.UserSignUpBo;
import com.virus.mall.model.UserUpdatePwd;
import com.virus.mall.model.vo.UserSignUpVo;
import com.virus.mall.service.UserService;
import com.virus.mall.utils.MybatisUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Classname UserServiceImpl
 * @Description NULL
 * @Date 2021/4/15 23:37
 * @Created by virus
 */
public class UserServiceImpl implements UserService {

    public List<User> allUser() {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        List<User> userList = mapper.allUser();
        MybatisUtils.releaseSession(session);
        return userList;
    }

    public boolean deleteUser(String id) {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        Integer affectedRow = mapper.deleteUser(Integer.parseInt(id));
        MybatisUtils.releaseSession(session);
        return affectedRow > 0;
    }

    public List<User> searchUser(String word) {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        List<User> userList = mapper.searchUser(word);
        MybatisUtils.releaseSession(session);
        return userList;
    }

    public UserSignUpVo signup(UserSignUpBo userSignUpBo) {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        User user = new User(null, userSignUpBo.getEmail(), userSignUpBo.getNickname(), userSignUpBo.getPwd()
                , userSignUpBo.getRecipient(), userSignUpBo.getAddress(), userSignUpBo.getPhone());
        Integer affectedRow = null;
        try {
            affectedRow = mapper.signup(user);
            session.commit();
        }catch (Exception e) {
            session.rollback();
            return null;
        } finally {
            session.close();
        }
        if (affectedRow <= 0) {
            session.rollback();
            session.close();
            return null;
        }
        UserSignUpVo userSignUpVo = new UserSignUpVo(user.getNickname(), user.getNickname());
        return userSignUpVo;
    }

    public Integer login(UserLoginBo userLoginBo) {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        User user = new User();
        user.setEmail(userLoginBo.getEmail());
        user.setPwd(userLoginBo.getPwd());
        Integer login = mapper.login(user);
        if (login == 1) {
            MybatisUtils.releaseSession(session);
            return 200;
        }
        session.close();
        return 404;
    }

    public User data(String token) {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        User user = mapper.data(token);
        MybatisUtils.releaseSession(session);
        return user;
    }

    public boolean updatePwd(UserUpdatePwd userUpdatePwd) {
        if (!userUpdatePwd.getNewPwd().equals(userUpdatePwd.getConfirmPwd())) {
            return false;
        }
        if (userUpdatePwd.getNewPwd().equals("")) {
            return false;
        }
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        Integer affectedRow = mapper.updatePwd(userUpdatePwd);
        MybatisUtils.releaseSession(session);
        return affectedRow > 0;
    }

    public boolean updateUserData(UpdateUserDataBo updateUserDataBo) {
        SqlSession session = MybatisUtils.getSession();
        UserDao mapper = session.getMapper(UserDao.class);
        try {
            if (updateUserDataBo.getAddress().equals("") || updateUserDataBo.getNickname().equals("")
                    || updateUserDataBo.getPhone().equals("") || updateUserDataBo.getRecipient().equals("")) {
                return false;
            }
            User user = new User();
            user.setId(updateUserDataBo.getId());
            user.setNickname(updateUserDataBo.getNickname());
            user.setRecipient(updateUserDataBo.getRecipient());
            user.setAddress(updateUserDataBo.getAddress());
            user.setPhone(updateUserDataBo.getPhone());
            Integer affectedRow = mapper.updateUserData(user);
            if (affectedRow <= 0) {
                return false;
            }
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
            return false;
        } finally {
            session.close();
        }
        return true;
    }
}
